package com.zhenwgang.listen_books.service;

import com.zhenwgang.listen_books.data_object.UserDO;
import org.mindrot.jbcrypt.BCrypt;
import com.zhenwgang.listen_books.repository.UserRepo;
import com.zhenwgang.listen_books.view_object.UserVO;

/**
 * 数据库中接收到的数据处理
 * @author Violet
 * @date 2022/07/07 09:45
 **/

public class UserService {
    private static final UserRepo userRepo = new UserRepo();

    public UserVO login(String username, String password){
        UserDO userDO = userRepo.selectOneByUserName(username);

        if (userDO == null){
            return null;
        }

        if (BCrypt.checkpw(password, userDO.password)){
            return new UserVO(userDO);
        } else {
            return null;
        }
    }

    public UserVO register(String username, String password){
        String salt = BCrypt.gensalt();
        password = BCrypt.hashpw(password, salt);

        UserDO userDO = new UserDO(username, password);
        userRepo.insert(userDO);

        return new UserVO(userDO);
    }
}